﻿using System;
using System.Drawing;
using Microsoft.DirectX.Direct3D;
using cz.zcu.PreSti.DirectX;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using cz.zcu.PreSti.XNADirectX;

namespace cz.zcu.PreSti.Components
{
    public class SoundStimulus : RunnableComponent
    {
        #region variables

        private XNADirectXPresentation present;
        private System.Drawing.Point position = System.Drawing.Point.Empty;
        private SizeF size = SizeF.Empty;
        private PointF rotationCenter = PointF.Empty;
        private float rotation = 0;
        private String soundFileName = "";

        private const String ERR_MSG = "Sound stimulus cannot work properly, because presentation object does not support audio output.";

        public const String CHOOSER_MSG = "Move chosen sound to the top...";
        public enum SoundNames {ring, scratching, beep, boom};

        #endregion

        #region parameters

        public System.Drawing.Point Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        public String SoundFilePath
        {
            get
            {
                return soundFileName;
            }
            set
            {
                soundFileName = value;
            }
        }

        public SizeF Size
        {
            get
            {
                return size;
            }
            set
            {
                size = value;
            }
        }

        public PointF RotationCenter
        {
            get
            {
                return rotationCenter;
            }
            set
            {
                rotationCenter = value;
            }
        }

        public float Rotation
        {
            get
            {
                return rotation;
            }
            set
            {
                rotation = value;
            }
        }

        #endregion

        public SoundStimulus(IPresentation p)
            : base(p)
        {
            if (p.supportsAudio())
            {
                if (p is XNADirectXPresentation)
                {
                    present = (XNADirectXPresentation)p;
                    present.initSound();
                }
                else
                {
                    Console.Out.WriteLine(ERR_MSG);
                }
                Finished += new EventHandler(onFinished);
            }
            else
            {
                Console.Out.WriteLine(ERR_MSG);
            }
        }

        private void onFinished(object sender, EventArgs e)
        {
            trigOut(0);
        }

        public override void trigIn(int n)
        {
            switch (n)
            {
                case 0:
                    base.trigIn(n);
                    if (present.IsTerminating == false)
                    {
                        playSound();
                    }
                    break;
                case 1:
                    terminate();
                    break;
            }
        }

        public override void update()
        {
            // nothing to do
        }

        private void playSound()
        {
            if (soundFileName.Equals(SoundStimulus.CHOOSER_MSG)
                || soundFileName.Trim().Equals(""))
            {
                soundFileName = SoundNames.beep.ToString();
            }
            present.startStopClock();
            present.playSound(soundFileName);
        }
    }
}
